<?php
$current_user = wp_get_current_user();
$uid = $current_user->user_login;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
<link href='http://fonts.googleapis.com/css?family=Headland+One' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<?php if(is_page('2')): ?>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/actions.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyANH601W3F-DHVEY8DnbHiz_3oLGF2ojag&sensor=true"></script>
<?php endif; ?>
<?php if ( is_singular() && get_option( 'thread_comments') ) wp_enqueue_script( 'comment-reply' ); ?>
<script type="text/javascript">
//<![CDATA[
var myOmap = [];
var Omap;
var lastI;

jQuery(document).ready(function(){
resizeIt();
<?php if(is_page('2')): ?>
var definfo = '<div class="mapinform">Nepal<br /><p class="mapInfoDesc">Okhati Maps</p></div>';

plot(27.7167, 85.3667, 7, "Okhati Maps for Nepal", definfo);
<?php endif; ?>

// Manipulating district and category checkboxes
// Show hide categories and districts dropdown
$("#catholder span").click(function(event){
if($("#searchdist").css("display") == "block")
$("#searchdist").slideToggle("fast");
$("#searchcat").slideToggle("fast");
});
$("#distholder span").click(function(event){
if($("#searchcat").css("display") == "block")
$("#searchcat").slideToggle("fast");
$("#searchdist").slideToggle("fast");
});
// Hide default checkbox to show background
$("#cat0").hide();
$("#dist0").hide();
// Select All Categories
$("#cat0").click(function(){
	checkboxWrapper($('#searchcat input[type="checkbox"]'),'cat0');
});
// Select All Districts
$("#dist0").click(function(){
	checkboxWrapper($('#searchdist input[type="checkbox"]'),'dist0');
});
// Checkbox clicked in category
$("#searchcat input[type='checkbox']").click(function(event){
	if($(this).attr('id') !== 'cat0'){
		if($(this).is(":checked") && $("#cat0").is(":checked"))
		$("#cat0").attr('checked',false);
		if($("#cat0").parent().is('div'))
		$("#cat0").unwrap().show();
	}
		wrapUnwrap(this);
	// Count
	if($('input[name="catlist[]"]:checked').length < 1){
	$("#cat0").attr('checked',true);
	$("#cat0").wrap('<div class="checkwrap"></div>').hide();
	}
});
$("#searchdist input[type='checkbox']").click(function(){
	if($(this).attr('id') !== 'dist0'){
		if($(this).is(":checked") && $("#dist0").is(":checked"))
		$("#dist0").attr('checked',false);
		if($("#dist0").parent().is('div'))
		$("#dist0").unwrap().show();
	}
		wrapUnwrap($(this));
	// Count
	if($('input[name="distlist[]"]:checked').length < 1){
	$("#dist0").attr('checked',true);
	$("#dist0").wrap('<div class="checkwrap"></div>').hide();
	}
});
// Close popups when clicked elsewhere
$("body").click(function(){
	if($("#searchcat").css('display') == "block")
	$("#searchcat").slideToggle("fast");
	if($("#searchdist").css('display') == "block")
	$("#searchdist").slideToggle("fast");
});
// But avoid closing when clicked within the box
$("#optholders").click(function(event){
event.stopPropagation();
});
// END OF READY
});
// Resize
jQuery(window).resize(function(){
resizeIt();
});

jQuery(".closeit").live('click',function(){
jQuery(this).parent().hide("slow");
});
//IGNORE 
jQuery(".fullheight img").live('click',function(e){
var body = '<p>These are important coordinates. Show some respect!</p>'+
			'<ul><li>X-coordinates: '+e.pageX+'</li>'+
			'<li>Y-coordinates: '+e.pageY+'</li>'+
			'</ul>';
showPopup('Your Coordinates',body);
});
// Check if scrollbar is present
function hasScrollbar() {
	var div= document.getElementById('Owrap');
	var docHeight = div.clientHeight;
	var scroll    = div.scrollHeight;
	//alert(scroll+'+'+docHeight);
	return (scroll > docHeight);
} 
//Resize Window
function resizeIt(){
	var offset = 0;
	if(hasScrollbar()){
		offset = 15; 
	}

	if(jQuery(window).width() >= 681){
	jQuery("#Owrap").css("height",jQuery(window).height()+"px");
	jQuery("#Owrap").css("width",jQuery(window).width()+"px");
	jQuery("#Omainbox").css("width",(jQuery(window).width() - 240 - offset)+"px");
	jQuery("#Omainnav").css("display","block");
	}
	else{
	jQuery("#Owrap").css("height",jQuery(window).height()+"px");
	jQuery("#Owrap").css("width",jQuery(window).width()+"px");
	jQuery("#Omainbox").css("width",jQuery(window).width() - offset +"px");
	jQuery("#Omainnav").css("display","none");
	}
}
//Show popup
function showPopup(head,body){
var innerContent;
innerContent = '<div class="popone"><div class="closeit">'+
				'<a href="javascript:void(0);"></a>'+
				'</div>';
innerContent += '<h3>'+head+'</h3>';
innerContent += '<div class="Osidecontent">'+body+'</div></div>';
jQuery("#Opopup").html(innerContent);
jQuery("#Opopup").fadeIn("slow");
}
//Process
function processIt(pageId,api){
var myurl;
if(api==2)
myurl = '/test/wp-content/themes/simplest/pulldata.php';
else
myurl = '/test/wp-content/themes/simplest/pullpage.php';
//AJAX
jQuery.ajax({
  type: "POST",
  url: myurl,
  data: { pageid:pageId, api: api },
  beforeSend: function(){jQuery("#ajaxloading").show();},
  complete: function(){jQuery("#ajaxloading").hide();}
}).done(function( msg ) {
  jQuery("#middleBox").html(msg);
  jQuery("#fullblackbg").fadeIn("slow");
});
}
//Slide Toggle
function toggleIt(divid){
jQuery('#'+divid).slideToggle("slow");
}
//AAD ----------------------------------
function insertForm(divid){

if($("#ansformdiv").length > 0)
$("#ansformdiv").remove();

var newform;
var qid;
qid = divid.split('-');

newform = '<div id="ansformdiv">';
newform += '<input type="hidden" name="ansid" id="ansid" value="'+qid[1]+'" />';
newform += '<textarea name="anstext" id="anstext"></textarea>';
newform += '<input type="submit" value="Answer" name="ansit" id="ansit" onclick="sendAnswer()" />';
newform += '<a id="hideansform" href="javascript:$(\'#ansformdiv\').hide(\'slow\')">Hide</a>';
newform += '</div>';

jQuery('#'+divid).append(newform);
}

function sendAnswer(){
<?php echo "var myhash = '" . md5($uid)."';";?>
	var ansid = $("#ansid").val();
	var answer = $("#anstext").val();
	var passdata = "aid="+ansid+"&ans="+encodeURI(answer)+"&h="+encodeURI(myhash);
	$.ajax({
	  type: "POST",
	  data: passdata,
	  url: "/test/wp-content/themes/simplest/postans.php",
	  //context: document.body,
	  beforeSend: function(){$("#ajaxloading").show();},
	  complete: function(){$("#ajaxloading").hide();}
	}).done(function(newdata){
	  $("#ansformdiv").hide("slow");
	  if(newdata.length > 1){
	  $("#qquest-"+ansid).html(newdata);
	  }
	  $("#qquest-"+ansid).show();
	});
}

function postQuestion(){
	var quesD = $("#quesd").val();
	var quesT = $("#quest").val();
	var anon = $("#anon").is(":checked")?'Yes':'No';
<?php echo "var myhash = '" . md5($uid)."';";?>
	var passdata = "quesD="+encodeURI(quesD)+"&quesT="+encodeURI(quesT)+"&h="+encodeURI(myhash)+"&anon="+anon;
	$.ajax({
	  type: "POST",
	  data: passdata,
	  url: "/test/wp-content/themes/simplest/postques.php",
	  //context: document.body,
	  beforeSend: function(){$("#ajaxloading").show();},
	  complete: function(){$("#ajaxloading").hide();}
	}).done(function(newdata){
		if(newdata == "Success")
		location.reload();
	});
}

function doqdel(qid,qs,q){
	<?php echo "var myhash = '" . md5($uid)."';";?>
	<?php echo "var qdel = ". (int) rand(1800, 2900) .";";?>
	var passdata = "qid="+qid+"&qs="+qs+"&h="+encodeURI(myhash)+"&qdel="+qdel;
	$.ajax({
	  type: "POST",
	  data: passdata,
	  url: "/test/wp-content/themes/simplest/postques.php",
	  //context: document.body,
	  beforeSend: function(){$("#ajaxloading").show();},
	  complete: function(){$("#ajaxloading").hide();}
	}).done(function(newdata){
		if(newdata == "Success")
		$("#addform-"+q).hide("slow");
		$("#qqest-"+q).hide();
		//console.log(newdata);
	});	
}
/*
Sujan's Javascript for Google Maps Plot
*/
//Plot one marker
var plot = function(lat, lon, zoom, title, desc) {
	  var latlng = new google.maps.LatLng(lat, lon);
	  //var katLatlng = new google.maps.LatLng(27.7167, 85.3667);
	  //var latlng = new google.maps.LatLng();
	  //var katLatlng = new google.maps.LatLng(27.7167, 85.3667);
	  var mapOptions = {
		zoom: zoom,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
	  }
	  var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	  var marker = new google.maps.Marker({
		  position: latlng,
		  title:title
		});
console.log(marker);
console.log(map);


	  var infowindow = new google.maps.InfoWindow({
			content: desc
		});
		
	  google.maps.event.addListener(marker, 'click', function() {
		  infowindow.open(map,marker);
		});
	  // To add the marker to the map, call setMap();
	  marker.setMap(map);
	  infowindow.open(map,marker);
}
// Process output of search query
function useData(data){
var mapData;	
var iconBase = 'http://okhati.org/test/wp-content/themes/simplest/img/';
try{
	mapData = jQuery.parseJSON(data);

	var avg = {
		lat: 0,
		lng: 0
	};
	var mapOptions = {
	zoom: 7,
	maxZoom: 17,
	center: new google.maps.LatLng(27.7167, 85.3667),
	mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	// Create Map
	var Omap = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	var bounds = new google.maps.LatLngBounds();
	
	var placelist = '<ul id="srchres">';
	// For each point returned from search
	jQuery.each(mapData, function(i, point){
		myOmap[i] = new Object;
		var latlng = new google.maps.LatLng(point["latitude"], point["longitude"]);
		var title = point["name"];
		
		var inform = '<div class="mapinform"><a class="mapInfoTitle" id="mapInfo-'+point["institution_id"]+'" href="javascript:void(0)" onclick="processIt('+point["institution_id"]+',2)">'+point["name"]+'</a><br />';
		inform += '<p class="mapInfoDesc">'+point["description"]+'</p>';
		
		if(point["APPROX"] == 1){
		inform += '<br /><img class="approxicon" src="images/warn.gif" alt="Approximate" title="Approximate" />';
		zlevel = 14;
		}
		inform += '</div>';
		var instype = point["institution_type"];

		// Marker
		var marker = new google.maps.Marker({
			position: latlng,
			map:Omap,
			visible:true,
			clickable:true,
			title:title,
			info: inform,
			icon: iconBase +"cat"+ instype + ".png",
			shadow: iconBase + 'shadow.png'
		});
		myOmap[i].marker = marker;
		// Infowindow content
		myOmap[i].infoWindow = new google.maps.InfoWindow({content:inform});
		// Attach even listner
		google.maps.event.addListener(marker, 'click', function () {
			if ( typeof(lastI) == 'number' && typeof(myOmap[lastI].infoWindow) == 'object' )
			{myOmap[lastI].infoWindow.close() ;}

			lastI = i ;    
			myOmap[i].infoWindow.open(Omap,marker);
		});
		bounds.extend(latlng);
		// Sum up all lat/lng to calculate center all points.
		  avg.lat += latlng.lat();
		  avg.lng += latlng.lng();
		// Create the list of results to display  
		placelist += '<li id="res'+i+'"><a href="javascript:void(0)" onclick="openInfoWindow('+i+')">'+point["name"]+'</a></li>';
	});
	placelist += '</ul>';
	
	// Center map
	//Omap.setCenter(new google.maps.LatLng(avg.lat / mapData.length, avg.lng / mapData.length));
	Omap.fitBounds(bounds);
}catch(e){
	placelist = data;
	clearMap()
	}
	// Display result box	
	//$('#Osearchresultholder').fadeIn("slow");
	if($("#Osearchresultholder").css('display') == "block"){
	$("#Osearchresultholder").animate({width:'250px'});
	}
	else{
	$("#Osearchresultholder").fadeIn("slow");
	$("#Ohideshowresult").show();
	}
	$("#colimg").attr('class','hideresultbox');
	$('#Osearchresult').html(placelist);	
}
// Open the infoWindow
function openInfoWindow(i)
{
	google.maps.event.trigger(myOmap[i].marker, 'click');
}
// Clear all markers in map
function clearMap() {
  jQuery.each(myOmap, function(i,obj){
    obj.marker.setMap(null);
  });
}
// Close search
function closesearch(){
$('#Osearchresultholder').fadeOut("slow");
}
// Check/uncheck checkbox
function checkboxWrapper(obj, item){
	$(obj).each(function(){
		if($(this).is(":checked")){
		$(this).attr("checked",false);
		wrapUnwrap($(this));
		}
	});
	$('#'+item).attr("checked",true);
	if($('#'+item).parent().not('div'))
	{
		$(this).wrap('<div class="checkbox"></div>').hide();
	}
}
// Put wrapper around checkbox to change background
function wrapUnwrap(obj){
	if($(obj).parent().is('div')){
		$(obj).unwrap().show();
	} else {
		$(obj).wrap('<div class="checkwrap"></div>').hide();
	}
}

// Expand/collapse search result when active
function collapsesearch(){
	if($("#Ohideshowresult").attr('class') == 'showresultbox'){
	$("#Osearchresultholder").show();
	$("#Osearchresultholder").animate({width:'250px'});
	$("#Ohideshowresult").attr("class","hideresultbox");
	}
	else{
	$("#Osearchresultholder").animate({width:'20px'});
	$("#Osearchresultholder").hide("medium");
	$("#Ohideshowresult").attr("class","showresultbox");
	}
}
//]]>
</script>
<style type="text/css">
<?php 
if(is_page('2'))
echo '#Opopup{display:block}';

//else
//echo '#Ocontent{border-top:1px solid #39f;top:50px;}';
?>
</style>
<?php wp_head(); ?>
</head>
<body>
<div id="fullblackbg">
<div class="closeit">
	<a href="javascript:void(0);"></a>
</div>
<div id="middleBox"></div></div>
<div id="Owrap">
<div id="ajaxloading"><img src="/test/wp-content/themes/simplest/img/okhati-loading.gif" alt="Loading..." /></div>